package com.example.game.hff;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;

import javax.imageio.ImageIO;

public class Player implements Constants{
	
	private Image image;//표시될 이미지
	private int x, y;//위치 정보
	private int dx, dy;//이동량
	private int sliceCount1 = 4;
	private int sliceCount2 = 4;
	private int imageWidth;
	private int imageHeight = 64;
	private int currentImagePosition;
	private int direction;//0:up, 1:down, 2:right, 3: left
	public static int skillPoint = 2; //필살기 횟수
	
	private long speed = 80;
	private long lastAnimatedTime = System.currentTimeMillis();
	
	public Player(Image image, int sliceCount) {
		this.image = image;
		x = 560;
		y = 730;
		
		this.imageWidth = image.getWidth(null) / 4;
		this.currentImagePosition = 0;
		this.direction = 1;
		

	}
	
	public void update() {
		x += dx;
		y += dy;
		
		if (x < 0) {
			x = 0;
		}
		else if (y < 50) {
			y = 50;
		}
		else if (x > SCREEN_WIDTH - imageWidth) {
			x = SCREEN_WIDTH - imageWidth;
		}
		else if (y > SCREEN_HEIGHT - imageHeight) {
			y = SCREEN_HEIGHT - imageHeight;
		}
		
		
	}
	
	
	public void draw(Graphics g) {
		int sx = imageWidth * currentImagePosition;
		int sy = imageHeight * direction;		
		
		g.drawImage(image, 
			x, y, x + imageWidth, y + imageHeight, sx, sy,
			sx + imageWidth, sy + imageHeight, null);
		
		if (dx != 0 || dy != 0) {			
			long current = System.currentTimeMillis();
			if (current - lastAnimatedTime > speed) {
				lastAnimatedTime = current;
				currentImagePosition++;
				
				if (dy != 0 && currentImagePosition >= sliceCount1)
					currentImagePosition = 0;
				if (dx != 0 && currentImagePosition >= sliceCount2)
					currentImagePosition = 0;
			}
		} 
	}
	
	
	
	public void keyPressedHandler(KeyEvent e) {
		int code = e.getKeyCode();
		
	loop : switch (code) {
		case KeyEvent.VK_LEFT : 
			direction = 1;
     		dx = -5;
			break;
		case KeyEvent.VK_RIGHT :

			direction = 2;
		    dx = 5;
			break;
		case KeyEvent.VK_UP : 

			direction = 3;
			dy = -5;
			break;
		case KeyEvent.VK_DOWN :

			direction = 0;
			dy = 5;
			break;
		case KeyEvent.VK_SPACE :
			if (skillPoint > 0) 
			{ x = 580; 
			y = 360; skillPoint--;
			}
			break;
		case KeyEvent.VK_SHIFT :
			if (direction == 2) {
				dx = 20;
			} else if (direction == 3) {
				dy = -20;
			} else if (direction == 0) {
				dy = 20;
			} else if (direction == 1) {
				dx = -20;
			}
		}
		
		
	}
	public void keyReleasedHandler(KeyEvent e) {
		int code = e.getKeyCode();
		switch (code) {
		case KeyEvent.VK_LEFT : 
			dx = 0;
			break;
		case KeyEvent.VK_RIGHT :
			dx = 0;
			break;
		case KeyEvent.VK_UP : 
			dy = 0;
			break;
		case KeyEvent.VK_DOWN : 
			dy = 0;
			break;
		case KeyEvent.VK_SHIFT :
			dx = 0;
			dy = 0;
		}
	}
	
	



	public int getX() {
		return x;
	}



	public void setX(int x) {
		this.x = x;
	}



	public int getY() {
		return y;
	}



	public void setY(int y) {
		this.y = y;
	}
	
	public int getWidth() {
		return image.getWidth(null) / 4;
	}
	
	public int getHeight() {
		return image.getHeight(null) / 4;
	}

}
